function logout() {
    Ext.Ajax.request({
        url: "../userlogin/logout",
        params: {},
        success:function(response, opts) {
            Ext.MessageBox.show({
                msg: '正在注销, 请等待...',
                progressText: '注销中...',
                width: 300,
                wait: {
                    interval: 200
                }
            });
            //window.location.reload()
            window.location="../static/login.html"
        }
    })
}

function doLogin() {
    Ext.Ajax.request({
        url: "../userlogin/store",
        params: {
            userLoginName:Ext.getCmp('user').getValue(),
            userLoginPassword:Ext.getCmp('pass').getValue(),
        },
        success:function(response, opts) {
            var res = Ext.JSON.decode(response.responseText)
            if (res.success) {
                //loginWindow.close()
                window.location.reload()
                Ext.MessageBox.show({
                    msg: '正在登录, 请等待...',
                    progressText: '登录中...',
                    width: 300,
                    wait: {
                        interval: 200
                    }
                });
            } else {
                alert(res.message)
            }
        }
    })
}

function openEditUserLoginWindow(title) {
    var data 
    if (Ext.getCmp('userLoginGrid').getSelection().length>0 && title!="增加用户") {
        data = Ext.getCmp('userLoginGrid').getSelection()[0].data
    } 
    console.log(data)
    var userLoginEditWindow = Ext.create('Ext.window.Window', {
        title: title,
        iconCls:'x-fa fa-database',
        width: 350,
        modal:true,
        items:[
            {
                xtype:'form',
                width:'100%',
                height:'100%',
                border:false,
                items:[
                    {
                        xtype: 'hiddenfield',
                        id:'userLoginId',
                        value:data==null?"":data.id
                    },
                    {
                        xtype: 'textfield',
                        labelAlign:'right',
                        allowBlank:false,
                        id:'userLoginName',
                        fieldLabel:'用户名',
                        value:data==null?"":data.user_login_name
                    },
                    {
                        xtype: 'textfield',
                        labelAlign:'right',
                        inputType: 'password',
                        id:'userLoginPassword',
                        allowBlank:false,
                        fieldLabel:'密码',
                        value:data==null?"":data.password
                    }
                ],
                buttons: [
                    {
                        xtype: 'button',
                        text: '取消',
                        handler: function() {
                            userLoginEditWindow.close()
                        }
                    },
                    {
                         xtype: 'button',
                         text: '确定',
                         handler: function() {

                            var userLoginName = Ext.getCmp("userLoginName").getValue()
                            var userLoginPassword = Ext.getCmp("userLoginPassword").getValue()
                            var userLoginId = Ext.getCmp("userLoginId").getValue()
                            
                            Ext.Ajax.request({
                                url: "../userlogin/save",
                                params: {
                                    userLoginId:userLoginId,
                                    userLoginName:userLoginName,
                                    userLoginPassword:userLoginPassword,
                                },
                                success:function(response, opts) {
                                    var res = Ext.JSON.decode(response.responseText)
                                    if (res.success) {
                                        userLoginEditWindow.close()
                                        userLoginStore.load()
                                    } else {
                                        alert(res.message)
                                    }
                                }
                            })

                           
                         }
                    }
                ]
            }
        ]
    })
    userLoginEditWindow.show()
}

var userLoginStore = Ext.create('Ext.data.Store', {
    autoLoad: true,
    pageSize: 111,
    remoteSort: true,
    remoteFilter:true,
    //sorters: [{property:"a.id",direction:"DESC"}],
    listeners:{
        beforeload:function() {
            
        }
    },
    proxy: {
        type: 'ajax',
        url : '../userlogin/store',
        reader: {
            type: 'json',
            root: 'results',
            totalProperty: 'total'
        }
    }
});
var userLoginWindow = Ext.create('Ext.window.Window', {
    title: '用户管理',
    iconCls:'x-fa fa-database',
    height: 500,
    width: 800,
    layout: 'fit',
    modal:true,
    closeAction:'hide',
    items: {  
        xtype: 'grid',
        id:'userLoginGrid',
        store:userLoginStore,
        viewConfig: {
            stripeRows: true,
            enableTextSelection: true
        },
        tbar: [
            { 
                xtype: 'button', text: '增加',iconCls:'x-fa fa-plus',
                handler:function() {
                    openEditUserLoginWindow('增加用户')
                    
                } 
            },
            { 
                xtype: 'button', text: '修改',iconCls:'x-fa fa-pencil',
                handler:function() {
                    openEditUserLoginWindow('修改用户')
                   
                } 
            },
            { 
                xtype: 'button', text: '删除',iconCls:'x-fa fa-minus',
                handler:function() {
                    var id = Ext.getCmp('userLoginGrid').getSelection()[0].data.id
                    Ext.Ajax.request({
                        url: "../userlogin/delete",
                        params: {id:id},
                        success:function(response, opts) {
                            userLoginStore.load()
                        }
                    })

                }   
            }
        ],
        columns: [
            { 
                text: '用户名', 
                dataIndex: 'user_login_name' 
            },
            { 
                text: '创建日期', 
                flex:1,
                dataIndex: 'date_created',
                renderer: function(value){
                    return Ext.Date.format(new Date(value), 'Y-m-d H:i')
                } 
            }
        ],
        height: 500,
        width: '100%',
        border: false
    }
})


var loginWindow = Ext.create('Ext.window.Window', {
    closable:false,
    title: '系统登录',
    //frame:true,
    width: 320,
    bodyPadding: 10,
    modal:true,
    closeAction:'hide',
    defaultType: 'textfield',
    items: [{
        allowBlank: false,
        fieldLabel: '用户名',
        name: 'user',
        id:'user',
        value:'',
        emptyText: '用户名',
        msgTarget: 'under'
    }, {
        allowBlank: false,
        fieldLabel: '密码',
        name: 'pass',
        id:'pass',
        value:'',
        emptyText: '密码',
        inputType: 'password'
    }],/*, {
        xtype:'checkbox',
        fieldLabel: 'Remember me',
        name: 'remember'
    }*/

    buttons: [
        /*{ text:'Register' },*/
        { text:'登录',handler:function(){doLogin()} }
    ],

    defaults: {
        anchor: '100%',
        labelWidth: 120
    }
})